#!/usr/bin/bash
version=7.0.14
installDir=/apps/redis
srcDir=/usr/local/src
user=redis

function installTools() {
    apt update
    apt-get install -y $@ || { echo "安装软件包失败，请检查网络配置" false; exit; }
}

function downloadAndUnzip() {
    cd ${srcDir}
    wget http://download.redis.io/releases/redis-${version}.tar.gz || { echo "Redis 源码下载失败" ; exit; }
    tar xf redis-${version}.tar.gz || { echo "Redis 源码解压失败" ; exit;}
}

function installRedis() {
    cd redis-${version}
    make PREFIX=${installDir} install && echo "Redis 编译安装完成" || { echo "Redis 编译安装失败" ;exit ;}
}

function configSetup() {
    ln -s ${installDir}/bin/redis-* /usr/bin/
    mkdir -p ${installDir}/{etc,logs,data,run}
    cp redis.conf ${installDir}/etc/ && echo "成功复制redis.conf文件" || { echo "redis.conf文件复制失败" ; exit;}
    #sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf
    sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/^dir .*/c dir ${installDir}/data/" -e "/logfile .*/c logfile ${installDir}/logs/redis-6379.log" -e "/^pidfile .*/c pidfile ${installDir}/run/redis_6379.pid" ${installDir}/etc/redis.conf

    if id ${user} &> /dev/null ;then
      echo "${user} 用户已存在"
    else
      useradd -r -s /sbin/nologin redis
      echo "${user} 用户创建成功"
    fi

    chown -R redis.redis ${installDir}

    overcommitHead=$(grep vm.overcommit_memory /etc/sysctl.conf | grep -o '#')
    if [[ ${overcommitHead} == '#' ]];then
        echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
    else
      overcommitValue=$(grep vm.overcommit_memory /etc/sysctl.conf | awk '{print $NF}')

      if [[ ${overcommitValue} == "" ]];then
        echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
      elif [[ ${overcommitValue} == "0" ]];then
        sed -i 's#vm.overcommit_memory = 0#vm.overcommit_memory = 1#' /etc/sysctl.conf
      fi
    fi

    somaxHead=$(grep net.core.somaxconn /etc/sysctl.conf | grep -o '#')
    if [[ ${somaxHead} == '#' ]];then
        echo 'net.core.somaxconn = 32768' >> /etc/sysctl.conf
    else
      somaxValue=$(grep net.core.somaxconn /etc/sysctl.conf | awk '{print $NF}')

      if [[ ${somaxValue} == "" ]];then
        echo 'net.core.somaxconn = 32768' >> /etc/sysctl.conf
      elif [[ ${somaxValue} < "1024" ]];then
        sed -i 's#net.core.somaxconn = '"${somaxValue}"'#net.core.somaxconn = 32768#' /etc/sysctl.conf
      fi
    fi

    sysctl -p

    #echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
    #chmod +x /etc/rc.d/rc.local
    #/etc/rc.d/rc.local

cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
#Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF
}

function startRedis() {
    systemctl daemon-reload
    chown -R redis.redis ${installDir}
    systemctl enable --now redis.service &> /dev/null && echo "Redis 服务启动成功" || { echo "Redis 启动失败" ;exit; }
}

function main() {
    installTools gcc
    downloadAndUnzip
    installRedis
    configSetup
    startRedis
}

main